

/*
 * @Author: Kang
 * @Date: 2022-02-12 18:03:51
 * @Last Modified by: Kang
 * @LastEditTime: 2022-06-30 17:03:52
 */
module.exports = app => {
    const { successRes } = require('../extend/helper');
    class NewsController extends app.Controller {

        /**
         * 后台接口开始
         */
        * index() {
            const { ctx, app } = this;
            try {
                const res = yield this.service.news.index(ctx.query, ctx.user.user_id);
                const result = {
                    data: res.rows,
                    total: res.count,
                    success: true,
                    current: parseInt(ctx.query.current),
                    pageSize: parseInt(ctx.query.pageSize)
                }
                ctx.body = successRes(result);
            } catch (error) {
                // throw new Error(error);
                ctx.body = {
                    status: 500,
                    desc: new Error(error),
                    data: null
                }
            }
        }

        * create() {
            const { ctx, app } = this;
            ctx.request.body.user_id = ctx.user.user_id
            ctx.request.body.create_time = new Date().getTime();
            ctx.request.body.update_time = new Date().getTime();
            console.log('ctx.request.body', ctx.request.body)
            const result = yield this.service.news.create(ctx.request.body)
            ctx.body = successRes(result);
        }


        * update() {
            try {
                const { ctx } = this;
                const result = yield this.service.news.update(ctx.request.body);
                ctx.body = successRes(result);
            } catch (error) {
                throw new Error(error);
            }
        }

        * destroy() {
            const { ctx } = this;
            console.log('ctx.params.id', ctx.query)
            const result = yield this.service.news.destroy(ctx.query.id);
            ctx.body = successRes(result);
        }
        /**
         * 后台接口结束
         */





        /**
         * 前台接口开始
         */
        * webNews() {
            const { ctx, app } = this;
            try {
                console.log('ctx.query', ctx.query);
                const result = yield this.service.news.webNews(ctx.query);
                ctx.body = {
                    code: 200,
                    msg: '提交成功',
                    data: result
                }
            } catch (error) {
                ctx.body = {
                    status: 500,
                    desc: '访问失败',
                    data: error
                }
            }
        }

        * getListAll() {
            const { ctx, app } = this
            try {
                const res = yield this.service.news.getListAll(ctx.query);
                const result = {
                    data: res.rows,
                    total: res.count,
                    msg: '查询成功',
                    current: parseInt(ctx.query.current),
                    pageSize: parseInt(ctx.query.pageSize)
                }
                ctx.body = result;
            } catch (error) {
                ctx.body = {
                    status: 500,
                    desc: '访问失败',
                    data: error
                }
            }
        }

        * getNewInfo() {
            const { ctx, app } = this
            try {
                const res = yield this.service.news.getNewInfo(ctx.query)
                const result = {
                    data: res,
                    msg: '查询成功'
                }
                ctx.body = result;
            } catch (error) {
                ctx.body = {
                    status: 500,
                    desc: '访问失败',
                    data: error
                }
            }
        }

        * getNewData() {
            const { ctx, app } = this
            console.log(ctx.query)
            try {
                const res = yield this.service.news.getNewData(ctx.query);
                const result = {
                    data: res,
                    msg: '查询成功',
                }
                ctx.body = result;
            } catch (error) {
                ctx.body = {
                    status: 500,
                    desc: '访问失败',
                    data: null
                }
            }
        }

        * getLatelyNews() {
            const { ctx, app } = this
            try {
                console.log(ctx.query)
                const res = yield this.service.news.getLatelyNews(ctx.query);
                const result = {
                    data: res,
                    msg: '查询成功',
                }
                ctx.body = result;
            } catch (error) {
                ctx.body = {
                    status: 500,
                    desc: '访问失败',
                    data: null
                }
            }
        }

    }
    /**
     * 前台接口结束
     */
    return NewsController;
}